<template>
  <el-dialog
    :visible.sync="dialog.编辑备注.visible"
    :close-on-click-modal="false"
  >
    <div slot="title" class="align-cm">
      <b class="fc-gray10">编辑备注</b>
    </div>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="备注">
        <el-input type="textarea" v-model="form.desc"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="cancel">取消</el-button>
      <el-button size="small" type="primary" @click="submit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { bus } from "@/main.js";
import { mapState, mapMutations } from "vuex";
export default {
  name: "editorsNote",
  data() {
    return {
      isDev: process.env.NODE_ENV == "development",
      form: {
        desc: "",
      },
    };
  },
  computed: {
    ...mapState(["dialog"]),
    arg: function () {
      return {
        id: this._.get(this.dialog.编辑备注.temp, "id", ""),
        remarks: this.form.desc,
      };
    },
  },
  watch: {
    "$store.state.dialog.编辑备注.visible": {
      deep: true,
      immediate: true,
      handler(nv, ov) {
        if (nv) {
          let remark = this._.get(this.dialog, "编辑备注.temp.remark", "");
          this.form.desc = remark;
        } else {
          this.form.desc = "";
        }
      },
    },
  },
  methods: {
    ...mapMutations(["toggleDialog"]),
    cancel: function () {
      this.toggleDialog({ type: "dialog", key: "编辑备注", tempData: null });
    },
    submit: function () {
      let arg = this.arg;
      this.$api
        .updateRemark(arg)
        .then((res) => {
          if (res.code == 200) {
            this.$message({
              message: "修改成功",
              type: "success",
            });
            return Promise.resolve({ id: arg.id, remarks: arg.remarks });
          }
        })
        .then((res) => {
          bus.$emit("updateRemark", { id: res.id, remarks: res.remarks });
          return Promise.resolve(200);
        })
        .then(() => {
          this.toggleDialog({
            type: "dialog",
            key: "编辑备注",
            tempData: null,
          });
        });
    },
  },
  updated: function () {},
};
</script>

<style>
</style>